<?php

/**
 *   OPENSHOP 销售明细列表程序
 * 
 *   @link        http://baison.com.cn
 *   @copyright   Baison, Inc.
 *   @package     OpenShop
 *   @version     $Id: sale_list.php,v 1.0 2009/06/17 09:55:58 modified $
 *   @author      FillBag <fillbag@hotmail.com>
 */

define('IN_OS', true);

require(dirname(__FILE__) . '/includes/init.php');
require_once(ROOT_PATH . 'includes/lib_order.php');

/* act操作项的初始化 */
if (empty($_REQUEST['act']))
{
    $_REQUEST['act'] = 'list';
}
else
{
    $_REQUEST['act'] = trim($_REQUEST['act']);
}

/*------------------------------------------------------ */
//--数据查询
/*------------------------------------------------------ */
/* 时间参数 */
if (isset($_POST) && !empty($_POST))
{
    $start_date = local_strtotime($_POST['start_date']);
    $end_date = local_strtotime($_POST['end_date']);
}
elseif (isset($_GET['start_date']) && !empty($_GET['end_date']))
{
    $start_date = local_strtotime($_GET['start_date']);
    $end_date = local_strtotime($_GET['end_date']);
}
else
{
    $today  = local_strtotime(local_date('Y-m-d'));
    $start_date = $today - 86400 * 7;
    $end_date   = $today;
}

if ($_REQUEST['act'] == 'down_sales_list')
{
    $start_date = $_GET['start_date'];
    $end_date   = $_GET['end_date'];
}

/* 查询数据的条件 */
$goods_sales_list = array();
$where = " WHERE og.order_id = oi.order_id". order_query_sql('finished', 'oi.') .
         " AND oi.add_time >= '$start_date' AND oi.add_time < '" . ($end_date + 86400) . "'";

$sql = 'SELECT og.goods_id, og.goods_sn, og.goods_name, og.goods_number AS goods_num, og.goods_price '.
       'AS sales_price, oi.add_time AS sales_time, oi.order_id, oi.order_sn '.
       "FROM " . $os->table('order_goods')." AS og, ".$os->table('order_info')." AS oi ".
       $where. " ORDER BY sales_time DESC, goods_num DESC";

$res = $db->query($sql);

while ($items = $db->fetchRow($res))
{
    $items['sales_price']   = price_format($items['sales_price']);
    $items['sales_time']    = local_date($_CFG['time_format'], $items['sales_time']);
    $goods_sales_list[]     = $items;
}

/*------------------------------------------------------ */
//--商品明细列表
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'list')
{
	/* 权限判断 */
	admin_priv('view_sale_list');
	
    /* 赋值到模板 */
    $smarty->assign('goods_sales_list', $goods_sales_list);
    $smarty->assign('start_date',   local_date('Y-m-d', $start_date));
    $smarty->assign('end_date',     local_date('Y-m-d', $end_date));

    $smarty->assign('ur_here',      '销售明细');    
    $smarty->assign('action_link',  array('text' => '下载销售明细',
    'href'=>'sale_list.php?act=down_sales_list&start_date='.$start_date.'&end_date='.$end_date));

    /* 显示页面 */
    assign_query_info();
    $from = trim($_REQUEST['from']);

    if ($from == '')
    {
        $from = trim($_POST['from']);
    }
    
    $smarty->assign('from',$from);    
    $smarty->display("sale_list.tpl");    
}

/*------------------------------------------------------ */
//--Excel文件下载
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'down_sales_list')
{
	/* 权限判断 */
	admin_priv('down_sales_list');
	
    $file_name = $_REQUEST['start_date'].'_'.$_REQUEST['end_date'] . '_sale';

    header("Content-type: application/vnd.ms-excel; charset=utf-8");
    header("Content-Disposition: attachment; filename=$file_name.xls");

    /* 文件标题 */
    echo os_iconv(OS_CHARSET, 'GB2312', local_date('Y-m-d', $_REQUEST['start_date']). '至' .local_date('Y-m-d', $_REQUEST['end_date']) . '销售明细') . "\t\n";

    /* 商品名称,订单号,商品数量,销售价格,销售日期 */
    echo os_iconv(OS_CHARSET, 'GB2312', '商品名称') . "\t";
    echo os_iconv(OS_CHARSET, 'GB2312', '订单号') . "\t";
    echo os_iconv(OS_CHARSET, 'GB2312', '数量') . "\t";
    echo os_iconv(OS_CHARSET, 'GB2312', '售价') . "\t";
    echo os_iconv(OS_CHARSET, 'GB2312', '售出日期') . "\t\n";

    foreach ($goods_sales_list AS $key => $value)
    {
        echo os_iconv(OS_CHARSET, 'GB2312', $value['goods_name']) . "\t";
        echo os_iconv(OS_CHARSET, 'GB2312', '[ ' . $value['order_sn'] . ' ]') . "\t";
        echo os_iconv(OS_CHARSET, 'GB2312', $value['goods_num']) . "\t";
        echo os_iconv(OS_CHARSET, 'GB2312', $value['sales_price']) . "\t";
        echo os_iconv(OS_CHARSET, 'GB2312', $value['sales_time']) . "\t";
        echo "\n";
    }
}
?>